//
//  BYRegisterView.m
//  School+
//
//  Created by Sean Sebastian on 4/26/16.
//  Copyright © 2016 Sean Sebastian. All rights reserved.
//

#import "BYRegisterView.h"

#import "Masonry.h"
#import "SVProgressHUD.h"


@implementation BYRegisterView

- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        [self prepareUI];
        [self.username_TF becomeFirstResponder];
    }
    return self;
}

- (void)prepareUI
{
    [self addSubview:self.backgroundImageView];
    [self addSubview:self.username_Label];
    [self addSubview:self.username_TF];
    [self addSubview:self.email_TF];
    [self addSubview:self.email_Label];
    [self addSubview:self.password_TF];
    [self addSubview:self.password_Label];
    [self addSubview:self.repassword_TF];
    [self addSubview:self.submitButton];
    [self addSubview:self.blank_Label];
    
    UIView *v1 = [[UIView alloc] init];
    v1.backgroundColor = [UIColor blackColor];
    UIView *v2 = [[UIView alloc] init];
    v2.backgroundColor = [UIColor blackColor];
    UIView *v3 = [[UIView alloc] init];
    v3.backgroundColor = [UIColor blackColor];
    UIView *v4 = [[UIView alloc] init];
    v4.backgroundColor = [UIColor blackColor];
    
    [self addSubview:v1];
    [self addSubview:v2];
    [self addSubview:v3];
    [self addSubview:v4];
    
//    [self.username_Label mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.mas_top).with.offset(80);
//        make.left.equalTo(self.mas_left).with.offset(20);
//    }];
//    
//    [self.username_TF mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.mas_top).with.offset(80);
//        make.left.equalTo(self.username_Label.mas_right).with.offset(20);
//        make.right.equalTo(self.mas_right).with.offset(-20);
//        
//    }];
    
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(0,0,0,0));
    }];
    
    
    
    NSArray *arr = @[ self.username_Label , self.username_TF];
    [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:260 leadSpacing:20 tailSpacing:20];
    [arr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).with.offset(100);
    }];
    
    [self.username_Label sizeToFit];

    
    [v1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.username_TF);
        make.height.mas_equalTo(@1);
        make.left.equalTo(self.username_TF);
        make.top.equalTo(self.username_TF.mas_bottom).with.offset(1);
    }];
    

#pragma mark - 还有一些控件也是这么添加，修改约束就行了
    arr = @[ self.password_Label , self.password_TF];
    [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:260 leadSpacing:20 tailSpacing:20];
    [arr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(v1).with.offset(20);
    }];
    
    [self.password_Label sizeToFit];
    
    
    [v2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.password_TF);
        make.height.mas_equalTo(@1);
        make.left.equalTo(self.password_TF);
        make.top.equalTo(self.password_TF.mas_bottom).with.offset(1);
    }];
    
    ///////
    
    arr = @[ self.blank_Label , self.repassword_TF];
    [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:260 leadSpacing:20 tailSpacing:20];
    [arr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(v2).with.offset(20);
    }];
    
    [self.blank_Label sizeToFit];
    
    
    [v3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.repassword_TF);
        make.height.mas_equalTo(@1);
        make.left.equalTo(self.repassword_TF);
        make.top.equalTo(self.repassword_TF.mas_bottom).with.offset(1);
    }];
    
    arr = @[ self.email_Label , self.email_TF];
    [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:260 leadSpacing:20 tailSpacing:20];
    [arr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(v3).with.offset(20);
    }];
    
    [self.email_Label sizeToFit];
    
    
    [v4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.email_TF);
        make.height.mas_equalTo(@1);
        make.left.equalTo(self.email_TF);
        make.top.equalTo(self.email_TF.mas_bottom).with.offset(1);
    }];

    [self.submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.email_Label);
        make.right.equalTo(self.email_TF);
        make.height.mas_equalTo(44);
        make.top.equalTo(v4).with.offset(30);
    }];
    
    
}

- (void)submitButtonClick
{
    if ([self.username_TF.text isEqualToString:@""] || [self.password_TF.text isEqualToString:@""] ||[self.repassword_TF.text isEqualToString:@""] || [self.email_TF.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"信息不能为空！"];
        return;
    }
    
    if(![self.password_TF.text isEqualToString:self.repassword_TF.text]){
        [SVProgressHUD showErrorWithStatus:@"确认密码不相同！"];
        return;
    }
    
    // 发送请求
    
    // url：不需要编码(内部已经实现了)
    NSURL *url = [NSURL URLWithString:@"http://localhost/SchoolPlus/userSignUp.php"];
    
    // 请求字符串
    NSString *bodyStr = [NSString stringWithFormat:@"username=%@&password=%@&email=%@",self.username_TF.text,self.password_TF.text,self.email_TF.text];
    // request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 设置请求方法
    request.HTTPMethod = @"POST";
 
    request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
        
        NSLog(@"%@--%@",response,result);
        
       [SVProgressHUD showSuccessWithStatus:result[@"errorMessage"]];
        
        return ;
        
    }];
    
    
    [self.delegate btnClick];
}

- (UILabel *)username_Label
{
    if (_username_Label == nil) {
        UILabel *label = [[UILabel alloc] init];
        label.text = @"用户名";
        
        _username_Label = label;
        return label;
    }
    return _username_Label;
}

- (UILabel *)password_Label
{
    if (_password_Label == nil) {
        UILabel *label = [[UILabel alloc] init];
        label.text = @"密码";
        
        _password_Label = label;
        return label;
    }
    return _password_Label;
}

- (UILabel *)email_Label
{
    if (_email_Label == nil) {
        UILabel *label = [[UILabel alloc] init];
        label.text = @"email";
        
        _email_Label = label;
        return label;
    }
    return _email_Label;
}

- (UILabel *)blank_Label
{
    if (_blank_Label == nil) {
        UILabel *label = [[UILabel alloc] init];
        label.text = @"  ";
        _blank_Label = label;
        return label;
    }
    return _blank_Label;
}


- (UITextField *)username_TF
{
    if (_username_TF == nil) {
        UITextField *tf = [[UITextField alloc] init];
        
        tf.alpha = 0.8f;
        tf.placeholder = @"请输入用户名";
        _username_TF = tf;
        return tf;
    }
    return _username_TF;
}

- (UITextField *)password_TF
{
    if (_password_TF == nil) {
        UITextField *tf = [[UITextField alloc] init];
        
        tf.alpha = 0.8f;
        tf.placeholder = @"请输入密码";
        tf.secureTextEntry = YES;
        
        _password_TF = tf;
        return tf;
    }
    return _password_TF;
}

- (UITextField *)repassword_TF
{
    if (_repassword_TF == nil) {
        UITextField *tf = [[UITextField alloc] init];
        tf.placeholder = @"再次输入密码";
        
        tf.alpha = 0.8f;
        tf.secureTextEntry = YES;
        
        _repassword_TF = tf;
        return tf;
    }
    return _repassword_TF;
}

- (UITextField *)email_TF
{
    if (_email_TF == nil) {
        UITextField *tf = [[UITextField alloc] init];
        tf.placeholder = @"请输入email";
        
        tf.alpha = 0.8f;
        _email_TF = tf;
        return tf;
    }
    return _email_TF;
}

- (UIButton *)submitButton
{
    if (_submitButton == nil) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.backgroundColor = [UIColor whiteColor];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.alpha = 0.8;
        [button setTitle:@"注册" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(submitButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        _submitButton = button;
        return button;
    }
    return _submitButton;
}

- (UIImageView *)backgroundImageView
{
    if (_backgroundImageView == nil) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"register"]];
        _backgroundImageView = imageView;
        return imageView;
    }
    return _backgroundImageView;
}

@end
